<?php

class Bemigo_FriendsController extends Bemigo_Controller_Action_Base
{

	public function init()
	{
		/* Initialize action controller here */
		$contextSwitch = $this->_helper->getHelper('AjaxContext');
		parent::init();
		$this->checkFBAuth();
	}

	public function indexAction()
	{
		$my_user_id = $this->_facebook->getUser();
		$this->view->user_id = $my_user_id;
		$em = $this->getEm();
		$dql = "SELECT u FROM Entities\Users u WHERE u.facebookId =?1";
		$query = $em->createQuery($dql)->setParameter(1, $my_user_id);
		$user = $query->getResult();
		if(!empty($user[0])){
			$dql = "SELECT f FROM Entities\Friends f WHERE f.user =?1";
			$query = $em->createQuery($dql)->setParameter(1, $user[0]);
			$relations = $query->getResult();
			if(!empty($relations)){
				foreach ($relations as $relation){
					$bemigos[] = $relation->getFriend();
				}
				$this->view->bemigos = $bemigos;
			}else{
				$this->_redirect("/mypage");
			}
		}else{
			//Exception HERE
		}
	}

}

